package com.example.piece.ui.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.dip2px
import com.example.base.ext.getViewModel
import com.example.base.ui.fragment.BaseFragment
import com.example.base.ui.view.DividerItemDecoration
import com.example.base.vm.onError
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.databinding.FragmentGuanZhuBinding
import com.example.piece.databinding.FragmentPingLunBinding
import com.example.piece.ui.adapter.MsgAdapter
import com.example.piece.ui.adapter.PingLunAdapter
import com.example.piece.vm.UserViewModel

/**
 *评论
 */
class PingLunFragment : BaseFragment<FragmentPingLunBinding>() {
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }
    private val adapter: PingLunAdapter by lazy {
        val adapter = PingLunAdapter()
        context?.let {
            mBinding.rv.addItemDecoration(DividerItemDecoration(context,
                DividerItemDecoration.HORIZONTAL_LIST,
                it.dip2px(1f).toInt(),
                Color.parseColor("#f4f4f4")))
        }
        mBinding?.rv?.adapter = adapter
        adapter
    }
    override fun getContentRes(): Int = R.layout.fragment_ping_lun

    companion object {
        fun newInstance(targetUserId: String): Fragment {
            val fragment = PingLunFragment()
            val bundle = Bundle()
            bundle.putString("targetUserId", targetUserId)
            fragment.arguments = bundle
            return fragment
        }
    }

    var page: Int = 1

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        val loadMoreModule = adapter.loadMoreModule
        loadMoreModule.setOnLoadMoreListener {
            getData()
        }
        adapter.setEmptyView(R.layout.layout_empty)
        mBinding?.srl?.setOnRefreshListener {
            page = 1
            loadMoreModule.isEnableLoadMore = true
            loadMoreModule.isAutoLoadMore = true
            getData()

        }
        getData()   }

    private fun getData() {
        userViewModel.userCommentList(this, page.toString()) {

            mBinding?.srl?.isRefreshing = false
            adapter.loadMoreModule.loadMoreComplete()

            if (it.code == 200) {
                page++
                if (it.data.isNullOrEmpty()) {
                    adapter.loadMoreModule.isEnableLoadMore = false
                    adapter.loadMoreModule.isAutoLoadMore = false
                    return@userCommentList
                }
                if (page == 2) {
                    adapter.setList(it.data)

                } else {
                    adapter.addData(it.data)

                }

            }
        } onError {
            mBinding?.srl?.isRefreshing = false

        }
    }

}